package com.fwtai.service.api;

import com.fwtai.api.ApiInformDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.service.AsyncService;
import com.fwtai.tool.ToolClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 通知|公告
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021年8月4日 15:02:35
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiInformService{

    @Resource
    private ApiInformDao apiInformDao;

    @Resource
    private AsyncService asyncService;

    public String listDataPage(final HttpServletRequest request){
        final PageFormData formData = ToolClient.pageParamsApi(request);
        if(formData == null) return ToolClient.jsonValidateField();
        Integer type = formData.getInteger("type");
        if(type == null){
            type = 1;
            formData.put("type",type);
        }
        final String loginId = LocalUserId.get();
        if(loginId != null){
            formData.put("loginId",loginId);
        }
        return ToolClient.jsonPage(apiInformDao.listDataPage(formData));
    }

    public String getInfoById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.createJsonFail("请求参数有误");
        }
        final HashMap<String,Object> result = apiInformDao.getInfoById(kid);
        final String loginId = LocalUserId.get();
        if(loginId != null){
            asyncService.updateReadInform(kid,loginId);
        }
        return ToolClient.queryJson(result);
    }

    public String getInformUnread(){
        return ToolClient.queryJson(apiInformDao.getInformUnread(LocalUserId.get()));
    }
}